package com.cskaoyan.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/04/02 09:51
 */
@Configuration
public class DataSourceConfiguration {

    /*@Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/cskaoyan_db");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }*/


    /*String driverClass = "com.mysql.jdbc.Driver";
    String jdbcUrl = "jdbc:mysql://localhost:3306/cskaoyan_db";
    String username = "root";
    String password = "123456";*/

    //is container component ,can use injection annotation
    //容器中的组件，可以使用注入功能注解

    @Value("${db.driver}")
    String driverClass;
    @Value("${db.url}")
    String jdbcUrl;
    @Value("${db.username}")
    String username;
    @Value("${db.password}")
    String password;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
